﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections.ObjectModel;

using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace RZD_maps.MapPath.Models
{
    public static class MapPathUtil
    {
        public static ObservableCollection<BaseCategory> CreatePath()
        {
            ObservableCollection<BaseCategory> list = new ObservableCollection<BaseCategory>();
            if (File.Exists("data.xml"))
            {
                DeserializeObject(list, "data.xml");
            }
            else
            {
                BaseCategory books = new BaseCategory("Маршрут 1");
                BaseCategory movies = new BaseCategory("Маршрут 2");
                BaseCategory music = new BaseCategory("Маршрут 3");

                //root categories
                list.Add(books);
                list.Add(movies);
                list.Add(music);
            }

            return list;
        }

        public static void SerializeObjectToXML<T>(T item, string FilePath)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            using (StreamWriter wr = new StreamWriter(FilePath))
            {
                xs.Serialize(wr, item);
            }
        }

        public static void SerializeObject(IEnumerable<BaseCategory> item, string FilePath)
        {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml("<root></root>");
            XmlElement root = xml.DocumentElement;
            foreach (BaseCategory itemval in item)
            {
                XmlElement elem = xml.CreateElement("route");
                XmlAttribute text = xml.CreateAttribute("name");
                text.Value = itemval.CategoryName;
                elem.SetAttributeNode(text);

                foreach (BaseCategory it in itemval.SubCategories)
                {
                    XmlElement elempoint = xml.CreateElement("point");
                    XmlAttribute txtName = xml.CreateAttribute("name");
                    txtName.Value = it.CategoryName;
                    elempoint.SetAttributeNode(txtName);
                    XmlAttribute txtLng = xml.CreateAttribute("lng");
                    txtLng.Value = it.Lng.ToString();
                    elempoint.SetAttributeNode(txtLng);
                    XmlAttribute txtLat = xml.CreateAttribute("lat");
                    txtLat.Value = it.Lat.ToString();
                    elempoint.SetAttributeNode(txtLat);
                    XmlAttribute txtVelocity = xml.CreateAttribute("velocity");
                    txtVelocity.Value = it.Velocity.ToString();
                    elempoint.SetAttributeNode(txtVelocity);
                    elem.AppendChild(elempoint);
                }
                root.AppendChild(elem);
            }
            xml.Save(FilePath); 
        }

        public static void DeserializeObject(ObservableCollection<BaseCategory> item, string FilePath)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(FilePath);
            XmlElement root = xml.DocumentElement;
            item.Clear();
            //foreach (BaseCategory dl in item)
            //{
            //    item.SubCategories.Remove(item);
            //};
            foreach (XmlElement el in root.ChildNodes)
            {
                string rtName = el.GetAttribute("name");
                BaseCategory route = new BaseCategory(rtName);
                item.Add(route);
                foreach (XmlElement it in el.ChildNodes)
                {
                    string ptName = it.GetAttribute("name");
                    string ptLng = it.GetAttribute("lng");
                    string ptLat = it.GetAttribute("lat");
                    string ptVelocity = it.GetAttribute("velocity");

                    BaseCategory cat = new BaseCategory(ptName, route);
                    double val = 0.0;
                    Double.TryParse(ptLng, out val);
                    cat.Lng = val;

                    val = 0.0;
                    Double.TryParse(ptLat, out val);
                    cat.Lat = val;

                    val = 0.0;
                    Double.TryParse(ptVelocity, out val);
                    cat.Velocity = val;
                    route.SubCategories.Add(cat);
                    cat.SubCategories.Add(new BaseCategory(string.Format("Долгота: {0}", cat.Lng), cat));
                    cat.SubCategories.Add(new BaseCategory(string.Format("Широта: {0}", cat.Lat), cat));
                    cat.SubCategories.Add(new BaseCategory(string.Format("{1}: Скорость : {0}", cat.Velocity, rtName), cat));
                }
            }
        }
        


    }
}
